package com.share.example.javafx.controller;

import com.share.example.javafx.view.HomeView;
import com.share.example.javafx.model.User;
import com.share.example.javafx.service.IUserService;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@FXMLController
public class LoginController {
    @FXML
    private TextField username;

    @FXML
    private TextField password;

    @Autowired
    private IUserService userService;

    @FXML
    private void btnLogin() {
        User user = userService.login(username.getText());
        if (user == null) {
            showMsg(null, "用户不存在");
        } else if (password.getText().equals(user.getPassword())) {
            log.info("登录成功");

            //显示下一个页面
            AbstractJavaFxApplicationSupport.showView(HomeView.class);
        } else {
            showMsg(null, "用户或账号密码错误");
        }
    }

    private void showMsg(TextField textField, String message) {
        log.info(message);
//        textField.setText(message);
    }
}
